# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 15:54:00 2020

@author: Farman
"""

import os
import sys

def list_dir(in_path, save_to_file):
    '''
    List all files and directories in path 'in_path',
    save file names to a file with name 'save_to_file'.

    Parameters
    ----------
    in_path : string
        Path of files and directories to be list .
    
    save_to_file : string
        File name to save files and directories list.

    Returns
    -------
    None.
    '''
    files = os.listdir(in_path)
    files.sort()
    files = [file + '\n' for file in files]
    
    with open(save_to_file, 'w') as f:
        f.write("Files and directories in [%s]\n"%(in_path))
        f.write('=' * 40 + '\n')
        f.writelines(files)
    
    return




if __name__ == '__main__':
    print('Files and directories lister.')
    print('=' * 40)
    
    if len(sys.argv) != 3:
        print('Parameters should be 2.\n')
        print('Usage: python3 listfiles.py in_path save_to_file')
    else:
        list_dir(sys.argv[1], sys.argv[2])
        print('Job done, result in [%s]'%sys.argv[2])